package com.li.drug.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.drug.entity.Owinfo;
import com.li.drug.service.OwinfoService;
import com.li.drug.utils.ResultMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 药品出入库相关controller
 */
@Controller
@RequestMapping("/owinfo")
public class OwinfoController {

    @Autowired
    OwinfoService owinfoService;

    /**
     * 转向药品出入库页面
     * @return
     */
    @RequestMapping
    public String owinfo(){
        return "owinfo/owinfo";
    }

    //分页查询药品出入库列表
    @RequestMapping("/owinfoQueryPage")
    @ResponseBody
    public Object owinfoQueryPage(String param,
                                    @RequestParam(name = "page") int pageNum,
                                    @RequestParam(name = "limit") int pageSize){
        try {
            Page<Owinfo> page = owinfoService.selectOwinfoPage(pageNum,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(page);

        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向药品出入库新增页面
     * @return
     */
    @RequestMapping("/owinfoPage")
    public String owinfoPage(){
        return "owinfo/owinfoPage";
    }

    /**
     * 添加一个药品出入库
     */
    @RequestMapping("/owinfoAdd")
    @ResponseBody
    public Object owinfo(Owinfo owinfo){
        owinfo.setCreatetime(new Date());
        int i = owinfoService.addOwinfo(owinfo);
        return ResultMapUtil.getHashMapSave(i);
    }

    /**
     * 转向药品出入库编辑页面
     * @return
     */
    @RequestMapping("/owinfoQueryById")
    public String owinfoEditById(@RequestParam(name = "id") Integer id,
                                   Model model){
        Owinfo owinfo = owinfoService.getOwinfoById(id);
        model.addAttribute("owinfo",owinfo);
        return "owinfo/owinfoPage";
    }

    /**
     * 修改一个药品出入库信息
     */
    @RequestMapping("/owinfoEdit")
    @ResponseBody
    public Object owinfoEdit(Owinfo owinfo){
        try {
            owinfo.setCreatetime(new Date());
            int i = owinfoService.editOwinfo(owinfo);
            return ResultMapUtil.getHashMapUpdate(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个药品出入库信息
     */
    @RequestMapping("/owinfoDelById")
    @ResponseBody
    public Object owinfoDelById(@RequestParam(name = "id") Integer id){
        try {
            int i = owinfoService.delOwinfoById(id);
            return ResultMapUtil.getHashMapDel(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有药品出入库列表
     */
    @RequestMapping("/owinfoList")
    @ResponseBody
    public Object owinfoList(){
        List<Owinfo> list = owinfoService.list();
        return list;
    }
}
